package wwk.com.android_meilishuo.mvp.model;

import android.content.Context;
import android.util.Log;

import com.wwk.core.net.Constants;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import wwk.com.android_meilishuo.interfaces.MLSInterface;
import wwk.com.android_meilishuo.ui.home.bean.HomeHeadBean;
import wwk.com.android_meilishuo.ui.home.bean.InfoBean;
import wwk.com.android_meilishuo.ui.home.bean.ListBean;

/**
 * Created by Administrator on 2017/1/6.
 */

public class AssortBiz implements MLSModel.IAssortBiz {
    private MLSInterface mMLSInterface;
    private List<ListBean> list_banner=new ArrayList<>();
    private List<ListBean> list_AllAssort=new ArrayList<>();
    private List<ListBean> list_Tag=new ArrayList<>();
    private List<ListBean> list_Category1=new ArrayList<>();
    private List<ListBean> list_Category2=new ArrayList<>();
    private List<ListBean> list_Category3=new ArrayList<>();
    private InfoBean infoBean_AllAssort=null;
    private InfoBean infoBean_Tag=null;
    private InfoBean infoBean_Category1=null;
    private InfoBean infoBean_Category2=null;
    private InfoBean infoBean_Category3=null;

    @Override
    public void loadMAssortData(final MLSModel.OnBannerListener onBannerListener, Context context) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL_MCE)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        mMLSInterface = retrofit.create(MLSInterface.class);
        Observable observable = mMLSInterface.getSortData();
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<HomeHeadBean>() {
            @Override
            public void call(HomeHeadBean homeHeadBean) {
                list_banner=homeHeadBean.getData().getBanner().getList();
                onBannerListener.onBannerSuccess(list_banner);
                list_AllAssort=homeHeadBean.getData().getCategoryAll().getList();
                onBannerListener.onAllAssortSuccess(list_AllAssort);
                infoBean_AllAssort=homeHeadBean.getData().getCategoryAll().getInfo();
                onBannerListener.onAllTitletSuccess(infoBean_AllAssort);
                list_Tag=homeHeadBean.getData().getTags().getList();
                onBannerListener.onTagtSuccess(list_Tag);
                infoBean_Tag=homeHeadBean.getData().getTags().getInfo();
                onBannerListener.onTagTitletSuccess(infoBean_Tag);

                list_Category1=homeHeadBean.getData().getCategory_1().getList();
                onBannerListener.onCategory1tSuccess(list_Category1);
                infoBean_Category1=homeHeadBean.getData().getCategory_1().getInfo();
                onBannerListener.onCategory1TitletSuccess(infoBean_Category1);

                list_Category2=homeHeadBean.getData().getCategory_2().getList();
                onBannerListener.onCategory2Success(list_Category2);
                infoBean_Category2=homeHeadBean.getData().getCategory_2().getInfo();
                onBannerListener.onCategory2TitletSuccess(infoBean_Category2);

                list_Category3=homeHeadBean.getData().getCategory_3().getList();
                onBannerListener.onCategory3Success(list_Category3);
                infoBean_Category3=homeHeadBean.getData().getCategory_3().getInfo();
                onBannerListener.onCategory3TitletSuccess(infoBean_Category3);

            }
        });

    }

    @Override
    public void onFailed(Exception e) {

    }
}
